package com.fjxd.flightNumber.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjxd.feign.clients.PlaneTicketClient;
import com.fjxd.flightNumber.mapper.FlightNumberMapper;
import com.fjxd.flightNumber.pojo.FlightNumber;
import com.fjxd.flightNumber.pojo.vo.HotCityVo;
import com.fjxd.flightNumber.service.FlightNumberService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FlightNumberServiceImpl extends ServiceImpl<FlightNumberMapper, FlightNumber> implements FlightNumberService {
    @Autowired
    private FlightNumberMapper flightNumberMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public List<Integer> getFlightNumberIdByCity(String city1, String city2) {
        String key = "flightNumber:" + city1 + city2;
        String jsonFlightNumber = redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(jsonFlightNumber)) {
            return JSONUtil.toList(jsonFlightNumber, Integer.class);
        }
        List<Integer> value = flightNumberMapper.getFlightNumberIdByCity(city1, city2);
        if (value.size() < 1) {
            return new ArrayList<>();
        }
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value));
        return value;
    }

    @Override
    public List<Integer> getFlightNumberIdByUpCityId(Integer upCityId, Integer downCityId, Integer startIndex, Integer size) {

        return flightNumberMapper.getFlightNumberIdByUpCityId(upCityId, downCityId, startIndex, size);
    }

    @Override
    public List<FlightNumber> getFlightNumberIdByDownCityId(Integer upCityId, Integer downCityId) {
        return flightNumberMapper.getFlightNumberIdByDownCityId(upCityId, downCityId);
    }

    @Override
    public List<FlightNumber> getFlightNumberByDownCity(Integer downCityId) {
        String key = "flightNumber:transfers:" + downCityId;
        String jsonFlightNumber = redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(jsonFlightNumber)) {
            return JSONUtil.toList(jsonFlightNumber, FlightNumber.class);
        }
        List<FlightNumber> value = flightNumberMapper.getFlightNumberByDownCity(downCityId);
        if (value.size() < 1) {
            return new ArrayList<>();
        }
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value));
        return value;
    }

    @Override
    public List<FlightNumber> getFlightNumberByUpCityIdAndDownCity(Integer upCityId, Integer downCityId, Integer startIndex, Integer size) {
        String key = "flightNumber:transfers:" + upCityId + downCityId;
        String jsonFlightNumber = redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(jsonFlightNumber)) {
            return JSONUtil.toList(jsonFlightNumber, FlightNumber.class);
        }
        List<FlightNumber> value = flightNumberMapper.getFlightNumberByUpCityIdAndDownCity(upCityId, downCityId, startIndex, size);
        if (value.size() < 1) {
            return new ArrayList<>();
        }
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value));
        return value;
    }

    @Override
    public List<FlightNumber>  getFlightNumberByUpCityAndDownCity(String upCity, String downCity) {
        return flightNumberMapper.getFlightNumberByUpCityAndDownCity(upCity,downCity);
    }

    @Override
    public FlightNumber getFlightNumberByFlightNumber(String flightNumber) {
        return flightNumberMapper.getFlightNumberByFlightNumber(flightNumber);
    }

    @Override
    public List<FlightNumber> getFlightNumberByPlaneId(Integer id) {
        LambdaQueryWrapper<FlightNumber> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(FlightNumber::getPId,id);
        return flightNumberMapper.selectList(queryWrapper);
    }

    @Override
    public List<FlightNumber> getplaneByFlightNumber(Integer pid) {

        return null;
    }

    @Override
    public List<HotCityVo> getHotCity(Integer id) {
        return  flightNumberMapper.getHotCity(id);
    }

    @Autowired
    public PlaneTicketClient planeTicketClient;
    @Override
    public List<FlightNumber> getTicketByCity(Integer cityId, String city) {
        List<FlightNumber> getTicketByCity = flightNumberMapper.getTicketByCity(cityId, city);
        for(FlightNumber flightNumber:getTicketByCity)
        {
            System.out.println(flightNumber.getDownCity());
            flightNumber.setTicket(planeTicketClient.getPlaneByFid(flightNumber.getId()));
        }

        return getTicketByCity;
    }

    @Override
    public List<FlightNumber> getManyMsg(List<Integer> id) {
        List<FlightNumber> flightNumbers=new ArrayList<>();
        for (int i: id){
            LambdaQueryWrapper<FlightNumber> queryWrapper=new LambdaQueryWrapper<>();
            System.out.println(i);
            queryWrapper.eq(FlightNumber::getId,i);
            FlightNumber flightNumber = flightNumberMapper.selectOne(queryWrapper);
            flightNumbers.add(flightNumber);
        }
        return flightNumbers;
    }

}
